/**
 * gis data store
 * manage all states of datasets
 * @author
 */
import {
  Module,
  Mutation,
  VuexModule,
  getModule,
  Action,
} from 'vuex-module-decorators'
import store from '@/store'
import {
  gisUploadShapeFiles,
  gisImportData,
  gisUploadTableFile,
  generateMask,
} from '@/api/gis-data'
import UserStore from '@/store/modules/user'
import { random } from 'lodash'
import { writeDataset } from '@/api/data'

// 数据集
export interface Dataset {
  id: number
  name: string
  type: string
}

// 数据集分类
export interface DatasetCategory {
  categoryId: number
  categoryName: string
  dataset: Array<Dataset>
}

@Module({ dynamic: true, namespaced: true, name: 'GisDataStore', store })
class GisDataStore extends VuexModule {
  /**
   * 上传批次号
   */
  private _batch: string = ''
  public get batch() {
    return this._batch
  }
  @Mutation
  public updateBatchNumber() {
    this._batch = `${
      UserStore.currentUser?.id || 0
    }-${new Date().getTime()}-${random(1000, 9999)}`
  }
  /**
   * 上传类型
   */
  private _uploadType: string = 'shapefile'
  public get uploadType() {
    return this._uploadType
  }
  @Mutation
  public setUploadType(value: string) {
    this._uploadType = value
  }
  /**
   * 上传类型为表格
   */
  private _uploadTypeTableFileType: string = ''
  public get uploadTypeTableFileType() {
    return this._uploadTypeTableFileType
  }
  @Mutation
  public setUploadTypeTableFileType(value: string) {
    this._uploadTypeTableFileType = value
  }

  private _fileList: any[] = [] // 已上传的文件
  public get fileList() {
    return this._fileList
  }
  @Mutation
  public setFileList(fileList: string[]) {
    this._fileList = fileList
  }

  /**
   * 坐标系
   */
  private _coordinateSystem: any = 4326
  public get coordinateSystem() {
    return this._coordinateSystem
  }
  @Mutation
  public setCoordinateSystem(value: any) {
    this._coordinateSystem = value
  }

  /**
   * 编码类型
   */
  private _codingType: string = 'gbk'
  public get codingType() {
    return this._codingType
  }
  @Mutation
  public setCodingType(value: string) {
    this._codingType = value
  }
  /**
   * 文件编码选项
   */
  private _datasetCodingTypes: Array<string> = [
    'utf-8',
    'ASCII',
    'GB18030',
    'Unicode',
  ]
  public get datasetCodingTypes() {
    return this._datasetCodingTypes
  }
  /**
   * 数据集名称
   */
  private _datasetName: string = ''
  public get datasetName() {
    return this._datasetName
  }
  @Mutation
  public setDatasetName(value: string) {
    this._datasetName = value
  }

  /**
   * 将第一行作为数据表的列字段名称
   */
  private _firstLineAsFields: boolean = true
  public get firstLineAsFields() {
    return this._firstLineAsFields
  }
  @Mutation
  public setFirstLineAsFields(value: boolean) {
    this._firstLineAsFields = value
  }

  /**
   * 上传数据预览， 未脱敏
   */
  public _previewData: any = null
  public get previewData() {
    return this._previewData
  }
  @Mutation
  public setPreviewData(value: any) {
    this._previewData = value
  }

  /**
   * 上传表格型数据集
   * @param passData
   */
  @Action
  public async uploadTableFile(passData: any) {
    try {
      const response = await gisUploadTableFile({ data: passData })
      // this.context.commit('setPreviewData', response.data.result)
      return response
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 上传 shapefile 文件
   * @param passData
   */
  @Action
  public async uploadShapeFiles(passData: any) {
    try {
      const response = await gisUploadShapeFiles({ data: passData })
      return response
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * shapefile 确认导入
   * @param passData
   */
  @Action
  public async gisImportData(passData: any) {
    try {
      const response = await gisImportData({ data: passData })
      return response
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 获取表格型数据预览（包含脱敏后）
   * @param postData
   */
  @Action
  public async generateMask(postData: any) {
    const response = await generateMask({ data: postData })
    return response
  }

  // 提交表格型数据集
  @Action
  public async writeGisFinalDataset(passData: any) {
    const writeResponse = await writeDataset({ data: passData })
    return writeResponse
  }
}

export default getModule(GisDataStore)
